package zcj.stu.controller;

import org.springframework.web.bind.annotation.*;
import zcj.stu.Entity.Category;
import zcj.stu.service.CategoryService;
import zcj.stu.service.CommonService;
import zcj.stu.utils.Result;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("category")
public class CategoryController {
    @Resource
    private CommonService commonService;
    @Resource
    private CategoryService categoryService;

    @GetMapping("/all")
    public Result all() {
        try {
            List<Category> categories = categoryService.all();
            return Result.ok(categories);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }

    }
    @DeleteMapping("/del/{_id}")
    public Result del(@PathVariable("_id") String _id){
        try {
            boolean isSuccess = categoryService.del(_id);
            if (isSuccess) {
                return Result.ok();
            } else {
                return Result.error();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }
    @PutMapping("/edit")
    public Result edit(@RequestBody Category category) {
        try {
            boolean isSuccess = categoryService.edit(category);
            if (isSuccess) {
                return Result.ok();
            } else {
                return Result.error();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    @PostMapping("/add")
    public Result add(@RequestBody Category category) {
        try {
            boolean isSuccess = categoryService.add(category);
            if (isSuccess) {
                return Result.ok();
            } else {
                return Result.error();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    @GetMapping("/getCategory")
    public Result getCategoryPage(int currentPage, int pageSize) {
        try {
            return commonService.getAccountPage(currentPage, pageSize, Category.class, null);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }
}
